﻿using A.M.Storage.Application;
using A.M.Storage.I_Infrastruecure;
using O.M.Production.Application.IService;
using O.M.Production.Domain.Entity;
using O.M.Production.ErrorCodeEnum;
using O.M.Production.Infrastuecure.IRepository;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace O.M.Production.Application.Service
{
    /// <summary>
    /// 工序流程关系表服务接口实现类
    /// </summary>
    public class working_procedure_Service : BaseServices<working_procedure, string>, I_working_procedure_Service
    {
        private readonly I_working_procedure_Repository _working_Procedure_Repository;
        private readonly I_process_flow_Repository _process_flow_Repository;
        private readonly I_working_procedure_manage_Repository _working_procedure_manage_Repository;
        public working_procedure_Service(I_working_procedure_Repository working_Procedure_Repository, I_process_flow_Repository process_flow_Repository, I_working_procedure_manage_Repository working_procedure_manage_Repository) : base(working_Procedure_Repository)
        {
            _working_Procedure_Repository = working_Procedure_Repository;
            _process_flow_Repository = process_flow_Repository;
            _working_procedure_manage_Repository = working_procedure_manage_Repository;
        }
        /// <summary>
        /// 分页显示-工序流程
        /// </summary>
        /// <param name="WPId_Name">工艺流程编号或名称</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">页码</param>
        /// <returns>统一返回格式</returns>
        public async Task<ApiResult> Get_working_procedure(string? WPId_Name, int pageSize, int pageIndex)
        {
            try
            {
                ApiResult apiResult = new ApiResult();

                var process_flow = await _process_flow_Repository.GetShowQueryableAsync().Result.ToListAsync();
                var working_procedure_manage = await _working_procedure_manage_Repository.GetShowQueryableAsync().Result.ToListAsync();
                var list = (from a in process_flow
                            join b in working_procedure_manage on a.process_flow_id equals b.process_flow_id
                            select new
                            {
                                b.working_procedure_id,
                                b.working_procedure_name,
                                a.process_flow_id,
                                a.process_flow_name,
                                a.process_quantity
                            }).AsQueryable();
                if (!string.IsNullOrEmpty(WPId_Name))
                {
                    list = list.Where(x => x.process_flow_id.Contains(WPId_Name) || x.process_flow_name.Contains(WPId_Name));
                }
                apiResult.totalCount = list.Count();
                apiResult.pageCount = (int)Math.Ceiling(list.Count() * 1.0 / pageSize);
                list = list.Skip(pageSize * (pageIndex - 1)).Take(pageSize);
                apiResult.Data = list.OrderByDescending(x => x.process_flow_id).ToList();
                return apiResult;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
    }
}
